#
# xmlbattle.py -- xml parsing for battle sim
# 
# author: Tom "Knio" Flanagan
#         knio@h4xn3t.net
#         theknio@gmail.com
#         irc.freenode.net - Knio - #python #pygame #py
#
# this file released under Public Domain
#

import mini_xml
import model.battle
import model.ship
import model.weapon

class XmlBattle:
    def __init__(self, file):
        self.xml = mini_xml.parse(file)
        self.battle = model.battle.Battle(**self.xml.attr)
        self.parse_children()


    def parse_children(self):
        for i in self.xml.children:
            if i.name == 'group': self.parse_group(i)
            
        
    def parse_group(self,xml):
        group = xml.attr
        for i in xml.children:
            if i.name != 'ship': continue
            self.parse_ship(i, group.copy())

    def parse_ship(self, xml, data):
        data.update(xml.attr)
        data['weapons'] = []
        ship = model.ship.Ship(self.battle, **data)
        for i in xml.children:
            self.parse_weapon(i, ship) 

    def parse_weapon(self, xml, ship):
        if xml.name == 'missile':
            ship.weapons.append(model.weapon.MissileLauncher(ship, **xml.attr))
        if xml.name == 'laser':
            ship.weapons.append(model.weapon.Laser (ship, **xml.attr))

            
            
